package br.com.rookgraph.utils;

import java.util.ArrayList;
import java.util.Collection;

import br.com.rookgraph.Edge;
import br.com.rookgraph.Graph;

public class Verificador {

	public static boolean fechaCiclo(Graph tree, Edge e) {
		int[] seg1 = tree.reached(e.origin+1, new int[tree.getOrder()]);
		int[] seg2 = tree.reached(e.target+1, new int[tree.getOrder()]);
		
		for (int vSeg1 : seg1) {
			for (int vSeg2 : seg2) {
				if (vSeg1 == vSeg2) {
					return true;
				}
			}
		}
		
		return false;
	}

	public static Collection<Edge> getEdges(Graph graph) {
		int v = graph.getOrder();
		
		Collection<Edge> edges = new ArrayList<Edge>();
		for (int i = 0; i < v; i++) {
			for (int j = i; j < v; j++) {
				if (graph.hasEdge(i+1, j+1)) {
					Edge e = new Edge(i, j);
					edges.add(e);
				}
			}
		}
		
		return edges;
	}
	
	public static void main(String[] args) {
		Graph graph = criarGrafo();
		
		System.out.println("GRAFO:");
		graph.showEdges();
		
		Graph treeKruskal = graph.getTreeKruskal(new Graph(graph.getOrder()));
		
		System.out.println("MST:");
		treeKruskal.showEdges();
	}

	private static Graph criarGrafo() {
		Graph g = new Graph(7);
		g.edges()[0][1] = 2;
		g.edges()[0][2] = 2;
		g.edges()[0][3] = 3;
		g.edges()[1][0] = 2;
		g.edges()[1][2] = 4;
		g.edges()[1][4] = 3;
		g.edges()[2][0] = 2;
		g.edges()[2][1] = 4;
		g.edges()[2][3] = 5;
		g.edges()[2][4] = 1;
		g.edges()[3][0] = 3;
		g.edges()[3][2] = 5;
		g.edges()[3][5] = 7;
		g.edges()[4][1] = 3;
		g.edges()[4][2] = 1;
		g.edges()[4][5] = 8;
		g.edges()[5][3] = 7;
		g.edges()[5][4] = 8;
		g.edges()[5][6] = 9;
		g.edges()[6][5] = 9;
		
		return g;
	}
	
}
